package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.enterprise.ServiceListEntiy;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.CompanyListEntity2;
import com.sgb.kjwl.model.entity.enterprise.CopyrightListEntity2;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.model.entity.enterprise.IntellpropEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedEntity;
import com.sgb.kjwl.model.entity.enterprise.PatentEntity;
import com.sgb.kjwl.model.entity.enterprise.PropertyEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.swgk.core.mmkvutil.CacheMarker;
import com.sgb.kjwl.view.ui.fragment.home.CorporateServicesFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CorporateServicesModel extends BaseViewModel {

    private HomeRepertory repertory;
    private CorporateServicesFragment fragment;
    private Map map;

    public CorporateServicesModel(HomeRepertory repertory, CorporateServicesFragment fragment) {
        this.repertory = repertory;
        this.fragment = fragment;
    }

    /**
     * 获取二级类别
     */
    public void getSecondType(int mItemType) {
        switch (mItemType) {
            case 0://服务大厅
                Type type = new TypeToken<List<ParentAreaEntity>>() {
                }.getType();
                Object object =  DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.ServiceListData + mItemType, type);
                if (object != null) {
                    List<ParentAreaEntity> list = (List<ParentAreaEntity>)object;
                    fragment.showServiceTypeView(list);
                    return;
                }
                EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceCategoryOption().enqueue(new Callback<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> call, Response<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> response) {
                        if (response.body() == null || response.body().getData() == null)
                            return;
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            List<ParentAreaEntity> dataList = new ArrayList<>();
                            List<ParentAreaEntity> list = new ArrayList<>();
                            list.addAll(response.body().getData());
                            dataList.addAll(response.body().getData());
                            String cacheMateialistlHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.ServiceListData + mItemType);
                            //获取从网络请求下来的数据的哈希值
                            String mHashNet = DatalUtils.getInstance().strToHashKey(list);
                            if (!cacheMateialistlHash.equals(mHashNet)) {
                                fragment.showServiceTypeView(dataList);
                                DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.ServiceListData + mItemType, list);
                            } else {
                                fragment.showServiceTypeView(dataList);
                            }
                        } else {
                            MToast.showToast(fragment.getContext(), response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
                break;
            case 1://需求大厅
                List<PickSelectEntity> list = new ArrayList<>();
                list.add(new PickSelectEntity("1", "商标需求"));
                list.add(new PickSelectEntity("2", "专利需求"));
                list.add(new PickSelectEntity("3", "版权需求"));
                //list.add(new PickSelectEntity("4", "公司"));
                list.add(new PickSelectEntity("5", "服务需求"));
                //list.add(new PickSelectEntity("6", "资产"));
                fragment.showDemandTypeView(list);
                break;
            case 2://知产交易
                List<PickSelectEntity> list1 = new ArrayList<>();
                list1.add(new PickSelectEntity("1", "商标"));
                list1.add(new PickSelectEntity("2", "版权"));
                list1.add(new PickSelectEntity("3", "专利"));
                //list1.add(new PickSelectEntity("4", "公司"));
                //list1.add(new PickSelectEntity("5", "资产"));
                fragment.showTransferTypeView(list1);
                break;
        }
    }

    /*
     * 服务大厅列表接口
     * */
    public void getProfessQuoteList(boolean isLoadMore, int page, String serviceClass,int mItemType) {
        if (page != 1){
            DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        }
        map = new HashMap();
        map.put("limit", 3);
        map.put("pageSize", page);
        map.put("serviceClass", serviceClass);
        map.put("asc",false);
        map.put("sortType",1);
        map.put("type","1");
       /* if (!fragment.getIsRefresh() && fragment.getPosition() == 0 && page == 1 && TextUtils.isEmpty(serviceClass)){
            Type type = new TypeToken<List<EpQuoteEntity>>() {
            }.getType();
            Object object =  DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.ServiceListData + "list"+ mItemType, type);
            if (object != null) {
                List<ServiceListEntiy> list = (List<ServiceListEntiy>)object;
                fragment.showQuoteList(list,isLoadMore);
                fragment.success();
                return;
            }
        }*/

        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceHallCompanyOrTeamList(map).enqueue(new Callback<BaseEntity<List<ServiceListEntiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ServiceListEntiy>>> call, Response<BaseEntity<List<ServiceListEntiy>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<ServiceListEntiy> list = response.body().getData();
                 /*   if (fragment.getPosition() == 0 && page == 1 && TextUtils.isEmpty(serviceClass)) {
                        String cacheMateialistlHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.ServiceListData + "list" + mItemType);
                        //获取从网络请求下来的数据的哈希值
                        String mHashNet = DatalUtils.getInstance().strToHashKey(list);
                        if (!cacheMateialistlHash.equals(mHashNet)) {
                            DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.ServiceListData + "list" + mItemType, list);
                        }
                    }*/
                    fragment.showQuoteList(list,isLoadMore);
                    fragment.success();
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                    fragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ServiceListEntiy>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*
     * 需求大厅列表接口
     * */
    public void getDemandHallList(boolean isLoadMore, int page, String requirementTypes) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("limit", 3);
        map.put("pageSize", page);
        map.put("requirementTypes", requirementTypes);
        APIManager.getInstance().getNeedAPI().getDemandHallList(map).enqueue(new Callback<BaseEntity<NeedEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<NeedEntity>> call, Response<BaseEntity<NeedEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getDemandHallList(response.body().getData().list, isLoadMore);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<NeedEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    /**
     * 商标列表查询
     */
    public void trademarkList(boolean isLoadMore, int page) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("limit", 6);
        map.put("page", page);
        NewAPIManager.getInstance().getEnterPrisenewAPI().trademarkLists(map).enqueue(new Callback<BaseEntity<ListResponse<List<IntellpropEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<IntellpropEntity>>>> call, Response<BaseEntity<ListResponse<List<IntellpropEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showTrademarkList(response.body().getData().getList(), isLoadMore);
                    fragment.success();
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                    fragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<IntellpropEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.error();
            }
        });
    }

    /**
     * 版权列表查询
     */
    public void getCopyrightList(boolean isLoadMore, int page) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("limit", 6);
        map.put("page", page);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getCopyrightQueryList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CopyrightListEntity2>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> call, Response<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showCopyrightList(response.body().getData().getList(), isLoadMore);
                    fragment.success();
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                    fragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.error();
            }
        });
    }

    /**
     * 专利列表查询
     */
    public void getPatentList(boolean isLoadMore, int page) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("limit", 6);
        map.put("page", page);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPatentList(map).enqueue(new Callback<BaseEntity<ListResponse<List<PatentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PatentEntity>>>> call, Response<BaseEntity<ListResponse<List<PatentEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showPatentList(response.body().getData().getList(), isLoadMore);
                    fragment.success();
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                    fragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PatentEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.error();
            }
        });
    }

    /**
     * 公司列表查询
     */
    public void getCompanyList(boolean isLoadMore, int page) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("limit", 3);
        map.put("page", page);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().companyList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CompanyListEntity2>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CompanyListEntity2>>>> call, Response<BaseEntity<ListResponse<List<CompanyListEntity2>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    fragment.getCompanyList(response.body().getData().getList(), isLoadMore);
                    fragment.success();
                } else {
                    Toast.makeText(fragment.getContext(), response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    fragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CompanyListEntity2>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                fragment.error();
            }
        });
    }

    public void getPropertyList(boolean isLoadMore, int page) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map = new HashMap();
        map.put("limit", 6);
        map.put("page", page);
        map.put("timeAscIndex", true);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().propertyList(map).enqueue(new Callback<BaseEntity<ListResponse<List<PropertyEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PropertyEntity>>>> call, Response<BaseEntity<ListResponse<List<PropertyEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    fragment.getPropertyList(response.body().getData().getList(), isLoadMore);
                    fragment.success();
                } else {
                    Toast.makeText(fragment.getContext(), response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    fragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PropertyEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                fragment.error();
            }
        });
    }
}
